package com.fsm.redisdm.uitls;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*******************
 *@Describe
 *@author 封神梦
 *@Time 2022/4/13
 *@Company http://com.yiview.com
 ****************/
public class JedisUtils {

    static JedisPool redisPool;

    public static final String USER = "user";

    /**
     * 获取jedis 连接池
     *
     * @return
     */
    static JedisPool getRedisPool() {
        if (redisPool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(20);      //最大空闲数
            config.setMaxTotal(10000);  //最大连接数
            config.setMinIdle(5);       //最小空闲数
            config.setMaxWaitMillis(10000L);     //最大等待时间
            redisPool = new JedisPool(config, "127.0.0.1", 6379);
        }
        return redisPool;
    }

    /**
     * 获取连接实例
     *
     * @return
     */
    public static Jedis getResource() {
        return getRedisPool().getResource();
    }

    /**
     * 插入字符串数据
     *
     * @param key   键值
     * @param value 内容
     * @return
     */
    public static boolean addString(String key, String value) {
        try {
            Jedis client = JedisUtils.getResource();
            String sRet = client.set(key, value);
            client.close();
            return sRet.equalsIgnoreCase("OK");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 获取字符串数据
     *
     * @param key 键值
     * @return
     */
    public static String getString(String key) {
        try {
            Jedis client = JedisUtils.getResource();
            String sRet = client.get(key);
            client.close();
            return sRet;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static boolean addHashData(String key, Map<String, String> collect) {
        try {
            Jedis client = JedisUtils.getResource();
            String sRet = client.hmset(key, collect);
            client.close();
            return sRet.equalsIgnoreCase("OK");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static boolean addHashData(String key, String id, Object data) {
        try {
            Jedis client = JedisUtils.getResource();
            Long hset = client.hset(key, id, JSON.toJSONString(data));
            client.close();
            return hset > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 移除数据
     *
     * @param key
     * @param id
     * @return
     */
    public static boolean removeHashData(String key, String id) {
        try {
            Jedis client = JedisUtils.getResource();
            Long hset = client.hdel(key, id);
            client.close();
            return hset > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 删除一组数据
     *
     * @param key 键值
     * @return
     */
    public static boolean clearData(String key) {
        try {
            Jedis client = JedisUtils.getResource();
            Long hset = client.del(key);
            client.close();
            return hset > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static <T> T getHashData(String key, String id, Class<T> clazz) {
        try {
            Jedis client = JedisUtils.getResource();
            Map<String, String> user = client.hgetAll(key);
            Set<String> keys = user.keySet();
            System.out.println("获取到对象：" + keys.size());
            String data = user.get(id);
            if (data != null) {
                T info = JSON.parseObject(data, clazz);
                return info;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 获取数据列表
     *
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> getHashDatas(String key, Class<T> clazz) {
        List<T> datas = new ArrayList<>();
        try {
            Jedis client = JedisUtils.getResource();
            Map<String, String> user = client.hgetAll(key);
            Set<String> keys = user.keySet();
            for (String skey : keys) {
                String sUser = user.get(skey);
                T info = JSON.parseObject(sUser, clazz);
                datas.add(info);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return datas;
    }

    public static boolean publsh(String key, String msg) {
        Jedis redis = JedisUtils.getResource();
        Long ret = redis.publish(key, msg);
        redis.close();
        return ret > 0;
    }
}
